package com.kandinfo.survey.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kandinfo.base.common.converter.ConverterService;
import com.kandinfo.base.common.service.AbstractService;
import com.kandinfo.survey.common.domain.QuestionOption;
import com.kandinfo.survey.common.dto.QuestionOptionDto;
import com.kandinfo.survey.common.repository.QuestionOptionRepository;
import com.kandinfo.survey.common.service.QuestionOptionService;

@Service
public class QuestionOptionServiceImpl extends AbstractService<QuestionOption, Long, QuestionOptionRepository> implements QuestionOptionService {

    private static final Logger logger = LoggerFactory.getLogger(QuestionOptionServiceImpl.class);
    
    @Resource
    public void setOptionRepository(QuestionOptionRepository questionOptionRepository) {
        super.setRepository(questionOptionRepository);
    }
    
    @Override
    @Transactional
    public Boolean saveOption(List<QuestionOptionDto> options) {
        if (CollectionUtils.isEmpty(options)) {
            return Boolean.FALSE;
        }
        
        List<QuestionOption> saveList = new ArrayList<>();
        try {
            options.forEach(optionDto -> {
                QuestionOption questionOption = new QuestionOption();

                questionOption = ConverterService.convert(optionDto, QuestionOption.class);
                saveList.add(questionOption);
            });
        } catch (Exception e) {
            logger.error(" newQuestion convert error - QuestionServiceImpl - save option ", e.toString());
            return Boolean.FALSE;
        }
        
        if (CollectionUtils.isNotEmpty(saveList)) {
            repository.save(saveList);
        }
        return Boolean.TRUE;
    }

}
